/*
 * Licensed Materials - Property of tenxcloud.com
 * (C) Copyright 2018 TenxCloud. All Rights Reserved.
 * 2018-11-26  @author lizhen
 */

package utility

import "reflect"

func Must(err error) {
	if err != nil {
		panic(err)
	}
}

type receiver interface {
	Value(interface{})
}

type holder struct {
	value interface{}
}

func (h holder) Value(receiver interface{}) {
	reflect.Indirect(reflect.ValueOf(receiver)).Set(reflect.ValueOf(h.value))
}

// As this utility method is used internal tenxcloud,
// So everyone who may use this, must be familiar with golang.
// Then they should take care with the 'receiver'.
// func someMethod(someArg string, otherArg int) (returnValue int, err error)
// for instance a method like above, then MustGet can used like below
// var returnValue int
// MustGet(someMethod("someArg", /* otherArg */ 888)).Value(&returnValue)
// and if someMethod's second return value (the err) was not nil
// the expression panic, other way the 'returnValue' should be the first
// return value of someMethod.
// YOU SHOULD take care of the receiver type yourself, if the 'var returnValue int'
// line goes to 'var returnValue string', then it also panic, since the first
// return value of someMethod is int not string.
func MustGet(anything interface{}, err error) receiver {
	Must(err)
	return &holder{value: anything}
}
